package com.nulldev.util.web.nCaptcha.captchas.identifcation.messedwords;

import java.awt.image.BufferedImage;

import com.nulldev.util.image.ImageUtil;
import com.nulldev.util.image2.textobf.TextObf;
import com.nulldev.util.image2.textobf.TextObf.ObfuscationMethod;
import com.nulldev.util.java.DataSet;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.web.nCaptcha.backend.Captcha;

public class MessedUpWordsCaptcha implements Captcha {
	
	public static String halfSplit(final String s) {
		final int half = s.length() / 2;
		final String s0 = s.substring(0, half);
		final String s1 = s.substring(half, s.length());
		return s1 + s0;
	}
	
	/**
	 * @returns [halfSplit() string, original]
	 */
	public static DataSet<String, String> halfRandom() {
		final String r = ArrayUtils.random(objects);
		return new DataSet<String, String>(halfSplit(r), r);
	}

	public static String[] objects = {
			"facebook", "media", "driver", "twitter", "instagram", "Audi", "Java", "nCaptcha",
			"company", "copyright", "notabot", "board", "human", "friend", "botnet", "post"
	};

	@Override
	public DataSet<String, String[]> generateBase64Image(final int botLevel) {
		final DataSet<String, String> r = halfRandom();
		final BufferedImage img = TextObf.obfuscate(r.getA(), (botLevel < 3 ? ObfuscationMethod.PixelObfuscation : 
			ObfuscationMethod.Noise));
		return new DataSet<String, String[]>("data:image/png;base64," + ImageUtil.convertImageDataBase64(img), new String[] {r.getB()});
	}

	@Override
	public String getType() {
		return "messedup";
	}
}
